package cn.yitun.chat.yitunserverimapicore.business.user.service.impl;

import cn.yitun.chat.yituncommonutil.YitunCollectionUtil;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.CrudServiceImpl;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.system.user.UserDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.system.user.UserInfo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.system.user.UserVo;
import cn.yitun.chat.yitunservercommonlib.enums.userChat.UserChatStatusEnums;
import cn.yitun.chat.yitunserverimapicore.business.user.service.UserService;
import cn.yitun.chat.yitunserverimbase.dao.system.UserDao;
import cn.yitun.chat.yitunserverimcore.bridge.main.business.user.store.UserStore;
import cn.yitun.chat.yitunserverimcore.general.main.biz.userchat.manager.UserManager;
import cn.yitun.chat.yitunserverimcore.general.main.push.user.UserPush;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Date 2019-01-20 12:35:32<br>
 * Description
 * @author yitun
 * @since 1.0.0
 */
@Service
@Transactional
@Slf4j
public class UserServiceImpl
		extends CrudServiceImpl<UserDao, UserDto, UserInfo, UserVo>
		implements UserService{


	@Resource
	private UserPush userPush;
	@Resource
	private UserStore userStore;
	@Resource
	private UserManager userManager;

	public UserVo getVO(Long userId) {
		return dao.getVO(userId);
	}

	/**
	 * Date 2019-01-20 13:07:08<br>
	 * Description 获取用户信息
	 * @author yitun
	 * @param id
	 * @return
	 * @since 1.0.0
	 */
	public UserVo getUserDataById(Long id) {
		UserVo userData = dao.getVO(id);
		return userData;
	}

	/**
	 * Date 2019-01-20 13:07:25<br>
	 * Description 条件分页查询
	 * @author yitun
	 * @param userQuery
	 * @return
	 * @since 1.0.0
	 */
	public List<UserVo> queryUserDataList(UserDto userQuery) {
		PageHelper.startPage(userQuery.getCurrentPageNo(), userQuery.getPageSize());

		List<UserVo> userDataList = dao.queryUserDataList(userQuery);
		return userDataList;
	}

	public void removeUser(Long userId) {
		userStore.putStatus(userId, UserChatStatusEnums.STATUS_OFFLINE.getType());
		sendUserChatOnlineStatus(userId, UserChatStatusEnums.STATUS_OFFLINE.getType());
	}

	public void setUserStatus(List<UserDto> userDataList) {
		if (YitunCollectionUtil.isNotEmpty(userDataList)) {
			for (UserDto userData : userDataList) {
				setUserStatus(userData);
			}
		}
	}

	public void setUserStatus(UserDto userData) {
		if (null != userData) {
			String status = userStore.getStatus(userData.getId());
			status = (YitunStringUtil.isEmpty(status)) ? UserChatStatusEnums.STATUS_OFFLINE.getType() : status;
			userData.setStatus(status);
		}
	}

	///////////////////////////////////////////////////////////////////////////////////////

	public void sendUserChatOnlineStatus(Long userId, String status) {
		userManager.updateChatOnlineStatus(userId, status);
	}


	/**
	 * 通过ids获取用户
	 * @param userIds
	 * @return
	 */
	public List<UserVo> getUserDataList(List<Long> userIds) {
		if (YitunCollectionUtil.isEmpty(userIds))
			return new ArrayList<>();

		return dao.getUserDataList(userIds);
	}
}
